<template>
  <div class="jeecg-basic-table station-list-view">
    <div class="tip">
      <span>监控列表</span>
      <span class="tip-img"></span>
    </div>
    <div class="jeecg-search-layer">
      <a-form ref="form" :model="queryParam">
        <a-row :gutter="24">
          <a-col :span="8">
            <a-form-item label="摄像头名称：" allowClear showSearch placeholder="请选择摄像头名称">
              <a-select v-model:value="queryParam.camId">
                <a-select-option value="">请选择</a-select-option>
                <template v-for="(vo, idx) in cameraList" :key="idx">
                  <a-select-option :value="vo.id">
                    {{ vo.name }}
                  </a-select-option>
                </template>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="站点">
              <StationSelect v-model:value="queryParam.stationCode" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24" style="text-align: right">
            <a-form-item>
              <a-button type="primary" @click="onSearch">查询</a-button>
              <a-divider type="vertical" />
              <a-button type="primary" @click="onAdd">新增</a-button>
              <a-divider type="vertical" />
              <a-button @click="onChangeMode">
                {{ mode == 'list' ? '卡片模式' : '列表模式' }}
              </a-button>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="jeecg-table-view">
      <template v-if="mode == 'list'">
        <a-table :loading="loading" rowKey="id" :dataSource="dataSource" :columns="columns" :pagination="false">
          <template v-slot:bodyCell="{ column, record, index }">
            <template v-if="column.dataIndex == 'action'">
              <a @click="onDetail(record)">查看</a>
              <a-divider type="vertical" />
              <a style="color: red" @click="onDelete(record)">删除</a>
            </template>
          </template>
        </a-table>
      </template>
      <template v-else>
        <div class="card-list">
          <a-row :gutter="[16, 16]">
            <template v-for="(record, index) in dataSource" :key="index">
              <a-col class="gutter-row" :span="8">
                <a-card :title="record.name" style="width: 100%">
                  <template #extra>
                    <a @click="onDetail(record)">编辑</a>
                    <a-divider type="vertical" />
                    <a style="color: red" @click="onDelete(record)">删除</a>
                  </template>
                  <p class="flex items-center justify-between">
                    <span>摄像头编码：</span>
                    <span>{{ record.camCode }}</span>
                  </p>
                  <p class="flex items-center justify-between">
                    <span>站点编码：</span>
                    <span>{{ record.stationCode }}</span>
                  </p>
                  <p class="flex items-center justify-between">
                    <span>录像机编码：</span>
                    <span>{{ record.nvrCode }}</span>
                  </p>
                  <p class="flex items-center justify-between">
                    <span>摄像抢账号：</span>
                    <span>{{ record.username }}</span>
                  </p>
                  <p class="flex items-center justify-between">
                    <span>通道：</span>
                    <span>{{ record.channel }}</span>
                  </p>
                  <p class="flex items-center justify-between">
                    <span>公网IP/端口：</span>
                    <span>{{ record.globalIp }}:{{ record.globalPort }}</span>
                  </p>
                  <p class="flex items-center justify-between">
                    <span>内网IP/端口：</span>
                    <span>{{ record.inIp }}:{{ record.inPort }}</span>
                  </p>
                </a-card>
              </a-col>
            </template>
          </a-row>
        </div>
      </template>
      <Pagination
        v-model:current="ipagination.current"
        :pageSize="ipagination.pageSize"
        :total="ipagination.total"
        show-less-items
        @change="handlePageChange"
        :hideOnSinglePage="true"
      />
    </div>
    <FormDrawer ref="formDrawer" @oks="oks" />
  </div>
</template>

<script lang="ts" setup>
import { ref, watch, getCurrentInstance, nextTick, onMounted } from 'vue';
import { cameraPageApi, cameraListApi, cameraDeleteApi } from '/@/api/camera';
import { Pagination } from 'ant-design-vue';
import { render } from '/@/utils/common/renderUtils';
import { JDictSelectTag } from '/@/components/Form';
import { StationSelect } from '/@/components/StationSelect';
import { useUserStore } from '/@/store/modules/user';
import FormDrawer from './components/form.vue';
import { useGo } from '/@/hooks/web/usePage';

const userStore = useUserStore();

const go = useGo();

let { proxy } = getCurrentInstance();

let loading = ref(false);

let queryParam = ref({
  camId: '',
  // 点位名称
  name: '',
  // 摄像抢编码
  camCode: '',
  // 内网IP
  inip: '',
  // 内网端口
  inPort: '',
  // 站点编码
  stationCode: '',
});

let ipagination = ref({
  current: 1,
  pageSize: 10,
  pageSizeOptions: ['10', '20', '30'],
  showTotal: (total, range) => {
    return range[0] + '-' + range[1] + ' 共' + total + '条';
  },
  showQuickJumper: true,
  showSizeChanger: true,
  total: 0,
});

// 列表模式 => list /卡片模式 => card
let mode = ref('list');

// 数据源
let dataSource = ref([]);

// 摄像头列表
let cameraList = ref([]);

// 字段
let columns = ref([
  {
    title: '序号',
    dataIndex: 'index',
    key: 'id',
    width: 60,
    align: 'center',
    customRender: (t, r, index) => {
      return (ipagination.value.current - 1) * ipagination.value.pageSize + t.renderIndex + 1;
    },
  },
  {
    title: '摄像头名称',
    align: 'center',
    dataIndex: 'name',
  },
  {
    title: '摄像头编码',
    align: 'center',
    dataIndex: 'camCode',
  },
  {
    title: '海康码',
    align: 'center',
    dataIndex: 'hikIndex',
  },
  {
    title: '录像机编码',
    align: 'center',
    dataIndex: 'nvrCode',
  },
  {
    title: '摄像头账号',
    align: 'center',
    dataIndex: 'username',
  },
  {
    title: '摄像头密码',
    align: 'center',
    dataIndex: 'password',
  },
  {
    title: '内网IP',
    align: 'center',
    dataIndex: 'inIp',
  },
  {
    title: '内网端口',
    align: 'center',
    dataIndex: 'inPort',
  },
  {
    title: '站点名称',
    align: 'center',
    dataIndex: 'stationCode',
    customRender: ({ text }) => {
      let data = userStore.getStationList.find((_) => _.stationCode == text);
      if (data) {
        return data.name;
      } else {
        return '-';
      }
    },
  },
  {
    title: '操作',
    dataIndex: 'action',
    align: 'center',
  },
]);

watch(
  () => mode.value,
  (e) => {
    if (e == 'list') {
      ipagination.value.pageSize = 10;
    } else {
      ipagination.value.pageSize = 9;
    }
    onInit();
  }
);

watch(
  () => queryParam.value.camId,
  (e) => {
    let data = cameraList.value.find((k) => k.id == e);
    let name = '';
    let camCode = '';
    if (data) {
      name = data.name;
      camCode = data.camCode;
    } else {
      name = '';
      camCode = '';
    }
    Object.assign(queryParam.value, {
      name,
      camCode,
    });
  }
);

/**
 * 初始化数据列表
 */
function onInit() {
  let { current, pageSize } = ipagination.value;
  let data = {
    current,
    size: pageSize,
  };
  loading.value = true;
  let keys = Object.keys(queryParam.value);
  Object.values(queryParam.value).map((_, i) => {
    if (_ !== '') {
      data[keys[i]] = _;
    }
  });
  delete data['camId'];
  cameraPageApi(data).then((res) => {
    loading.value = false;
    let { records, size, current, total } = res;
    Object.assign(ipagination.value, {
      current,
      pageSize: size,
      total,
    });
    dataSource.value = records;
  });
}

/**
 * 初始化摄像头列表
 */
function initCameraList() {
  cameraListApi({}).then((res) => {
    cameraList.value = res;
  });
}

/**
 * 切换当前页数
 */
function handlePageChange(page) {
  ipagination.value.current = page;
  onInit();
}

/**
 * 打开新增弹窗
 */
function onAdd() {
  proxy.$refs.formDrawer.visible = true;
}

/**
 * 查看详情
 */
function onDetail({ id }) {
  proxy.$refs.formDrawer.visible = true;
  nextTick(() => {
    proxy.$refs.formDrawer.init(id);
  });
}

/**
 * 跳转到视频播放页面
 */
function onPreview({ id }) {
  go(`/camera/play/${id}`);
}

/**
 * 删除数据
 */
function onDelete({ id }) {
  cameraDeleteApi({ id }).then((res) => {
    onInit();
  });
}

/**
 * 搜索数据
 */
function onSearch() {
  ipagination.value.current = 1;
  onInit();
}

function oks() {
  onInit();
  initCameraList();
}

/**
 * 切换卡片或者列表模式
 */
function onChangeMode() {
  mode.value = mode.value === 'list' ? 'card' : 'list';
}

onMounted(() => {
  initCameraList();
  onInit();
});
</script>

<style lang="less" scoped>
.station-list-view {
  margin: 12px;
  background: #fff;
  .tip {
    width: 100%;
    height: 44px;
    padding: 0 0 0 43.3px;
    background: url('@/assets/images/img-17.png') no-repeat;
    background-size: 100% 100%;
    line-height: 44px;
    span {
      font-size: 16px;
      color: #fff;
    }
  }
  .jeecg-search-layer {
    margin: 0 0 17px;
    padding: 12px 15px;
    background: #fff;
    border-radius: 8px;
    .ant-form {
      max-width: auto;
      .ant-form-item {
        margin-bottom: 24px;
      }
      :deep(.ant-col) {
        width: auto;
      }
    }
  }
  .jeecg-table-view {
    margin: 24px;
    .card-list {
      padding: 0 0 24px;
    }
    .ant-pagination {
      padding: 12px 0;
    }
  }
}
</style>